package com.blb.community.controller.ZyOwner;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.blb.community.pojo.ResponseResult;
import com.blb.community.pojo.VO.OwnerAdd;
import com.blb.community.pojo.VO.OwnerUpdate;
import com.blb.community.pojo.VO.UpdatePwd;
import com.blb.community.pojo.ZyOwner;
import com.blb.community.service.ZyOwnerService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Random;

@RestController
@RequestMapping("/zy")
public class ZyOwnerController {
    @Resource
    private ZyOwnerService zyOwnerService;
    @Resource
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    private Random random=new Random();
    private char[] zm="qwertyuiopasdfghjklzxcvbnm123456789-".toCharArray();
    @PutMapping("/registerOwner")
    public ResponseResult register(@RequestBody OwnerAdd ownerAdd){
        ZyOwner zyOwner = new ZyOwner();
        zyOwner.setOwnerPhoneNumber(ownerAdd.getUsername());
        zyOwner.setOwnerPassword(bCryptPasswordEncoder.encode(ownerAdd.getPassword()));
        StringBuffer stringBuffer=new StringBuffer();
        for (int i = 0; i < 16; i++) {
            stringBuffer.append(zm[random.nextInt(zm.length)]);
        }
        zyOwner.setOwnerNickname(stringBuffer.toString());
        zyOwnerService.save(zyOwner);
        return ResponseResult.ok("注册成功");
    }

    @GetMapping("/owner")
    public ResponseResult getOwner(@RequestParam String phone){
        ZyOwner owner = zyOwnerService.getOne(new QueryWrapper<ZyOwner>().eq("owner_phone_number", phone));
        if(owner.getOwnerRealName()!=null&&(!owner.getOwnerRealName().equals(""))){
            owner=zyOwnerService.getOwner(phone);
        }
        return ResponseResult.ok(owner);
    }
   @PutMapping("/owner")
    public ResponseResult updateOwner(@RequestBody OwnerUpdate ownerUpdate){
        if(!ownerUpdate.getData().equals("")&&ownerUpdate.getData()!=null) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate localDate = LocalDate.parse(ownerUpdate.getData(), formatter);
            ownerUpdate.setDataReal(localDate);
        }
         zyOwnerService.updateOwner(ownerUpdate);
         return ResponseResult.ok("修改成功");
   }
   @PutMapping("/owner-pwd")
    public ResponseResult updatePwd(@RequestBody UpdatePwd updatePwd){
       ZyOwner owner = zyOwnerService.getOne(new QueryWrapper<ZyOwner>().eq("owner_id", updatePwd.getOwnerId()));
       if(!bCryptPasswordEncoder.matches(updatePwd.getOldPwd(),owner.getOwnerPassword())){
           return ResponseResult.error(401L,"旧密码输入错误");
       }
       zyOwnerService.update(new UpdateWrapper<ZyOwner>().set("owner_password",bCryptPasswordEncoder.encode(updatePwd.getNewPwd())).eq("owner_id",updatePwd.getOwnerId()));
       return ResponseResult.ok("修改密码成功");
   }

}
